#include "textsprite.h"

TextSprite::TextSprite(const char *fname,std::string _text,
                       SDL_Color _frente,SDL_Color _fondo,int size)
{
    fprintf(stderr, "TextSprite(%d): cargando '%s' pt %d ... ",
            this,fname,size);

    font = NULL;
    image = new Image();
    needs_reupdate = false;

    text = _text;

    x = y = 0;
    frame_n = 0;

    frente = _frente;
    fondo =  _fondo;

    font = TTF_OpenFont(fname,size);
    if(font == NULL)
    {
        fprintf(stderr," Error : %s\n",SDL_GetError());
        exit(EXIT_FAILURE);
    }
    else
        fprintf(stderr," OK!\n");

    image->surf = TTF_RenderText_Solid(font,text.c_str(),frente);

    frame_rect = image->surf->clip_rect;
}

TextSprite::~TextSprite()
{
    TTF_CloseFont(font);
}

void
TextSprite::update(unsigned int time_delta)
{

    if(needs_reupdate){

        if(image->surf)
            SDL_FreeSurface(image->surf);

        image->surf = TTF_RenderText_Solid(font,text.c_str(),frente);
        frame_rect = image->surf->clip_rect;
        frame_n = 0;
    } 
     needs_reupdate=false;
}

SDL_Rect
TextSprite::getRect()
{
    return image->surf->clip_rect;
}

void
TextSprite::setText(std::string _text)
{
    text = _text;
    needs_reupdate = true;
}

std::string
TextSprite::getText()
{
    return text;
}
/* ex: set cindent expandtab softtabstop=4 shiftwidth=4 tabstop=4: */
